<?php
// 通过手机端获取微博数据的接口。主要用于v4升级的话题模块

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Weibo_phone_json extends CI_Controller {
	
	const SINA          = 'sina';
	const YUNYUN        = 'yunyun';
	const NETEASE       = 'netease';
	const SOHU          = 'sohu';
	const TENCENT       = 'tencent';
	const PLATFORM      = 'platform';
	const ADMIN         = 'soften';
	const ERROR_TAB     = 'error';
	const LOG_LEVEL_ERROR = 'error';
	const LOG_LEVEL_WARING = 'waring';
	const LOG_LEVEL_NOTICE = 'notice';
	public $_log_dir    = 'keyword';
	public $_result_dir = 'result';
	public $_return_data;
	
	public function __construct(){
		parent::__construct();
		$this->load->helper('MY_auth_verification');
		$this->load->library('error_code_library');
		$this->load->library('crawl_abstract');
		//$this->load->database();
		$this->_return_data = $this->config->item('return_data');
	}

	function content() {
		$this->authenticate();

		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('crawl_sina_from_phone_json_library');
		$this->load->library('crawl_sina_content_from_phone_json_library');

		if(empty($_POST['mid'])) {
			$this->json_msg(401, '参数错误');
		}

		$rs = $this->crawl_sina_content_from_phone_json_library->crawl();

		if(false === $rs) {
			$this->json_msg(500, '未抓到内容');
		}else{
			$this->json_msg(200, $rs);
		}
	}

	function author() {

		$this->load->database();
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('crawl_sina_from_phone_json_library');
		$this->load->library('crawl_sina_author_from_phone_json_library');


		if(empty($_POST['uid'])) {
			$this->json_msg(401, '参数错误');
		}

		$rs = $this->crawl_sina_author_from_phone_json_library->crawl();
		$author = array();
		if(isset($rs->user)) {
			$user = $rs->user;
			$author['followers_count'] = intval($user->followers_count);
			$author['friends_count'] = intval($user->follow_count);
			$author['status_count'] = intval($user->statuses_count);
			$author['gender'] = $user->gender;
			$author['name'] = $user->screen_name;
			$author['verified'] = intval($user->verified);
			$author['verified_reason'] = $user->verified_reason;
			$author['profile_url'] = $user->profile_url;
			$author['avator_img'] = $user->profile_image_url;
			$author['location'] = '';
		}
		$this->load->library('crawl_sina_author_location_from_phone_json_library');
		$rs = $this->crawl_sina_author_location_from_phone_json_library->crawl();
		if($rs) {
			$author['location'] = $rs;
		}
		if(empty($rs)) {
			$this->json_msg(500, '未找到博主信息');
		}else{
			$this->json_msg(200, $author);
		}
	}
	
	
	function comments() {
		$this->authenticate();
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('crawl_sina_from_phone_json_library');
		$this->load->library('crawl_sina_comments_from_phone_json_library');

		if(empty($_POST['url'])) {
			$this->json_msg(401, '参数错误');
		}

		$url_setments = parse_url($_POST['url']);
		$url = 'http://m.weibo.cn/'.$url_setments['path'];
		$content = file_get_contents($url);
		if(empty($content)) {
			$this->json_msg(501, '未获取到微博内容');
		}
		if(preg_match('/"mid": "(\d*)"/ims', $content, $match)) {
			$_POST['mid'] = $match[1];
		}

		// 默认页
		$page = $this->input->post('page');
		if(empty($page))
		{
			$_POST['page'] = 1;
		}else{
			$_POST['page'] = intval($_POST['page']);
		}

		$rs = $this->crawl_sina_comments_from_phone_json_library->crawl();
		if(false === $rs) {
			$this->json_msg(500, '未找到评论信息');
		}else{
			$this->json_msg(200, $rs);
		}
	}

	function json_msg($code, $data='')
	{
		echo json_encode(array(
			'code' => $code,
			'data' => $data
		));
		die;
	}

	function authenticate() {
		$auth_verification_result = authentication($this, $this->input->post());
		if(is_array($auth_verification_result) && array_key_exists(self::ERROR_TAB, $auth_verification_result)) {
			echo json_encode($auth_verification_result);
			exit;
		}
	}
	

}